package com.ruoyi.web.controller.business;

import java.util.List;

import com.ruoyi.business.domain.BusContract;
import com.ruoyi.business.domain.BusContractCostDetail;
import com.ruoyi.business.domain.BusContractCostParam;
import com.ruoyi.business.service.IBusContractService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.BusContractCost;
import com.ruoyi.business.service.IBusContractCostService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 合同成本Controller
 * 
 * @author ruoyi
 * @date 2021-09-02
 */
@RestController
@RequestMapping("/business/cost")
public class BusContractCostController extends BaseController
{
    @Autowired
    private IBusContractCostService busContractCostService;

    @Autowired
    private IBusContractService busContractService;

    /**
     * 查询合同成本列表
     */
    //@PreAuthorize("@ss.hasPermi('business:cost:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusContractCost busContractCost)
    {
        startPage();
        List<BusContractCost> list = busContractCostService.selectBusContractCostList(busContractCost);
        return getDataTable(list);
    }

    /**
     * 查询合同成本列表(index 页面接口)
     */
    //@PreAuthorize("@ss.hasPermi('business:cost:listContractCost')")
    @GetMapping("/listContractCost")
    public TableDataInfo listContractCost(BusContractCostParam busContractCostParam)
    {
        startPage();
        List<BusContractCostDetail> list = busContractCostService.getContractCostList(busContractCostParam);
        return getDataTable(list);
    }

    /**
     * 导出合同成本明细列表
     */
    //@PreAuthorize("@ss.hasPermi('business:cost:export')")
    @Log(title = "合同成本", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BusContractCost busContractCost)
    {
        BusContract busContract = busContractService.selectBusContractByContractId(busContractCost.getContractId());
        List<BusContractCost> list = busContractCostService.selectBusContractCostList(busContractCost);
        ExcelUtil<BusContractCost> util = new ExcelUtil<BusContractCost>(BusContractCost.class);
        return util.exportExcel(list, "合同编号【"+busContract.getContractNo()+"】成本信息");
    }

    /**
     * 导出合同成本列表
     */
    //@PreAuthorize("@ss.hasPermi('business:cost:exportAll')")
    @Log(title = "合同成本", businessType = BusinessType.EXPORT)
    @GetMapping("/exportAll")
    public AjaxResult exportAll(BusContractCostParam busContractCostParam)
    {
        List<BusContractCostDetail> list = busContractCostService.getContractCostList(busContractCostParam);
        ExcelUtil<BusContractCostDetail> util = new ExcelUtil<BusContractCostDetail>(BusContractCostDetail.class);
        return util.exportExcel(list, "合同成本信息");
    }

    /**
     * 获取合同成本详细信息
     */
    //@PreAuthorize("@ss.hasPermi('business:cost:query')")
    @GetMapping(value = "/{costId}")
    public AjaxResult getInfo(@PathVariable("costId") Long costId)
    {
        return AjaxResult.success(busContractCostService.selectBusContractCostByCostId(costId));
    }

    /**
     * 新增合同成本
     */
    //@PreAuthorize("@ss.hasPermi('business:cost:add')")
    @Log(title = "合同成本", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusContractCost busContractCost)
    {
        busContractCost.setCreateBy(getUsername());
        return toAjax(busContractCostService.insertBusContractCost(busContractCost));
    }

    /**
     * 修改合同成本
     */
   // @PreAuthorize("@ss.hasPermi('business:cost:edit')")
    @Log(title = "合同成本", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusContractCost busContractCost)
    {
        busContractCost.setUpdateBy(getUsername());
        return toAjax(busContractCostService.updateBusContractCost(busContractCost));
    }

    /**
     * 删除合同成本
     */
   // @PreAuthorize("@ss.hasPermi('business:cost:remove')")
    @Log(title = "合同成本", businessType = BusinessType.DELETE)
	@DeleteMapping("/{costIds}")
    public AjaxResult remove(@PathVariable Long[] costIds)
    {
        return toAjax(busContractCostService.deleteBusContractCostByCostIds(costIds));
    }

}
